package com.ycsj.goldmedalnewconcept.adapter;

import android.content.Context;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;



import java.util.List;

import com.ycsj.goldmedalnewconcept.R;

import com.ycsj.goldmedalnewconcept.adapter.CommonAdapter;
import com.ycsj.goldmedalnewconcept.adapter.ViewHolder;
import com.ycsj.goldmedalnewconcept.bean.CourseInfo;

/**
 * 创建日期：2017/11/3  10:31
 * 描述: 显示课程进度的GridView的适配器
 * 作者:张伟
 */

public class CourseGvAdapter extends CommonAdapter<CourseInfo.Data> {
    //屏幕宽高
    private int screenWidth;

    //上下文
    private Context context;

    //线性布局参数
    LinearLayout.LayoutParams lp;

    public CourseGvAdapter(Context context, List<CourseInfo.Data> datas, int layoutId, int screenWidth) {
        super(context, datas, layoutId);
        this.screenWidth = screenWidth;
        this.context = context;
        lp = new LinearLayout.LayoutParams((screenWidth- dp2px(5 * 8)) / 4 , (screenWidth- dp2px(5 * 8)) / 4);
    } 

    @Override
    public void convert(ViewHolder holder, CourseInfo.Data data) {
        RelativeLayout rlOut = holder.getView(R.id.rl_out);
        rlOut.setLayoutParams(lp);
        if (data.flag) {
            rlOut.setBackgroundResource(R.drawable.course_selected);
        } else {
            rlOut.setBackgroundResource(R.drawable.course_normal);
        }
        //设置文字
        TextView tv = holder.getView(R.id.tv);
        tv.setText(data.getName());
    }


    /**
     * 定义将dp转化为px的方法
     *
     * @param dp
     * @return
     */
    public int dp2px(int dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }

}
